package com.wy.tranFile.util;


/**
 * @author 吴宇~
 * @motto 我亦无他，唯手熟尔。
 * @edition jdk1.8
 * @reflection 0-1 喧闹任其喧闹，自由我自为之，我自风情万种，与世无争。
 * @date 2024/06/29/22:49
 * @target 心若有所向往，何惧道阻且长
 */
public class FileNameUtils {
    private static final String FILENAME_SUFFIX = ".ok";
    private static final String FILENAME_MIDDLE_FIX = "_middle_";
    private static final String FILENAME_PREFIX = "file_";

    public static String doOkFileName(String uuid, String fileName) throws Exception {
        String name = FILENAME_PREFIX + DecryptionUtil.encrypt(uuid) +FILENAME_MIDDLE_FIX+ fileName + FILENAME_SUFFIX;
        return name;
    }

    public static boolean checkFileName(String fileName,String parentPathUuid)  {
        boolean b = fileName.startsWith(FILENAME_PREFIX);
        boolean b1 = fileName.endsWith(FILENAME_SUFFIX);
        if (b&&b1){
            String uuid = null;
            try {
                uuid = DecryptionUtil.decrypt(fileName.substring(FILENAME_PREFIX.length() , fileName.indexOf(FILENAME_MIDDLE_FIX)));
            } catch (Exception e) {
                System.out.println(parentPathUuid+"文件夹文件找不到");
                return false;
            }
            if (uuid.equals(parentPathUuid)){
                return true;
            }
        }
        return false;
    }

    public static String getRealFileName(String fileName){
        int i = fileName.indexOf(FILENAME_MIDDLE_FIX);
        int i1 = i + FILENAME_MIDDLE_FIX.length();
        int length = FILENAME_SUFFIX.length();
        int i2 = fileName.length() - length;
        String substring = fileName.substring(i1, i2);
        return substring;

    }
}
